package controller

import (
	"hft/Database/admin"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

type Route struct{}

func (r *Route) AddRoute(c *gin.Context) {

	var routes *admin.Route

	c.ShouldBindJSON(&routes)

	if err := services.RouteService.AddRoute(routes); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}

}

func (r *Route) EditRoute(c *gin.Context) {
	var routes *admin.Route

	c.ShouldBindJSON(&routes)

	if err := services.RouteService.EditRoute(routes); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}

}

func (r *Route) DeleteRoutes(c *gin.Context) {

	var ids *request.IDS

	c.ShouldBindJSON(&ids)

	if err := services.RouteService.DeleteRoutes(ids.IDS); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

func (r *Route) GetRoutes(c *gin.Context) {

	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage("授权失败", c)
	} else {
		if routes, err := services.RouteService.GetRoutes(claims.BaseClaims); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {

			var treeRoutes = utils.BuildRoutesTree(routes, 1)

			utils.OkWithData(treeRoutes, c)
		}
	}

}

var RouteController = new(Route)
